<?php

namespace App\Admin\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Plan;
use App\Models\Project;
use App\Models\Student;
use Illuminate\Http\Request;
use Carbon\Carbon;

class BoardController extends Controller
{
	public function studentList()
	{
		$data = Student::with('plan')->get()->toArray();
		$result = formResultOptions();
		foreach ($data as $k => $v) {
			$data[$k]['name'] = self::maskSecondCharacter($v['name']);
			$data[$k]['age'] = Carbon::parse()->diffInYears($v['birthday']);
			$data[$k]['plan_name'] = $v['plan']['name'] ?? '-';
			$data[$k]['result_text'] = $result[$v['control_result']] ?? '-';
		}
		return $this->success($data);
	}

	public function signStudent()
	{
		$v = Student::with('project')->find(1)->toArray();
		$v['name'] = self::maskSecondCharacter($v['name']);
		$v['age'] = Carbon::parse()->diffInYears($v['birthday']);
		$v['sex'] = formSexOptions()[$v['sex']] ?? '男';
		$v['project_name'] = $v['project']['name'];
		return $this->success($v);
	}

	public function totalBoard()
	{
		$data = [
			'total' => 17458,
			'rate_target' => 97.3,
			'rate_danger' => 0.32
		];
		return $this->success($data);
	}

	public static function maskSecondCharacter($name) {
		// 判断是否全部为中文
		if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $name)) {
			// dd(strlen($name), $name);
			if (strlen($name) == 6) {
				return substr($name, 0, 3) . '*';
			} else if (strlen($name) >= 9) {
				return substr($name, 0, 3) . '*' . substr($name, 6);
			} else {
				return $name;
			}
		} else {
			return $name;
		}
	}
}